const News = require("../../models/news.model");
class NewsService {
  async createNews(news) {
    const res = await News.create(news);
    return res.dataValues;
  }

  async findPbNews() {
    const res = await News.findAll({
      where: { isPublish: 1 },
      order: [["editTime", "DESC"]],
    });
    return res;
  }
  async findANew({ id }) {
    const res = await News.findOne({ where: { id } });

    return res !== null ? res : null;
  }
  async findLimitNews({ limit }) {
    const parsedLimit = Number(limit); //必须为数字型
    try {
      const res = await News.findAll({
        where: { isPublish: 1 },
        order: [["editTime", "DESC"]],
        limit: parsedLimit,
      });
      return res;
    } catch (err) {
      console.log(err);
    }
  }
}

module.exports = new NewsService();
